import React from 'react'
import PropTypes from 'prop-types'
import { Form, Modal, Row, Col, InputNumber, Icon, Input, Select } from 'antd'
import style from './hr.less'
import { SelectProps } from 'utils'

const FormItem = Form.Item
const Option = Select.Option
const { TextArea } = Input

const formItemLayout = {
  labelCol: {
    span: 10,
  },
  wrapperCol: {
    span: 14,
  },
}
const memoLayout = {
  labelCol: {
    span: 5,
  },
  wrapperCol: {
    span: 19,
  },
}
const modal = ({
  item = {},
  onOk,
  form: {
    getFieldDecorator,
    validateFields,
    getFieldsValue,
  },
  currenciesData,
  ...modalProps
}) => {
  const handleOk = () => {
    validateFields((errors) => {
      if (errors) {
        return
      }
      let data = {}
      if (item.id) {
        const { id, currencyCodeFrom, currencyCodeTo, currencyNameFrom, currencyNameTo } = item
        data = {
          ...getFieldsValue(),
          id,
          currencyCodeFrom,
          currencyCodeTo,
          currencyNameFrom,
          currencyNameTo,
        }
      } else {
        data = {
          ...getFieldsValue(),
        }
      }
      console.log(data)
      onOk(data)
    })
  }
  const modalOpts = {
    ...modalProps,
    onOk: handleOk,
  }

  const createUpdateUI = () => {
    return (<Row className={style.FromandTo}>
      {item.currencyCodeFrom}
      <Icon type="swap-right" />
      {item.currencyCodeTo}
      <hr className={style.modalcutLine} />
    </Row>)
  }
  const createNewUI = () => {
    const typeData = currenciesData.map(d => <Option key={d.code}>{d.code}</Option>)
    return (<Row>
      <Col span={12}>
        <FormItem label="currencyFrom" hasFeedback {...formItemLayout}>
          {getFieldDecorator('currencyCodeFrom', {
            initialValue: item.currencyCodeFrom,
            rules: [
              {
                required: true,
              },
            ],
          })(<Select style={{ width: '100%' }} {...SelectProps}>{ typeData }</Select>)}
        </FormItem>
      </Col>
      <Col span={12}>
        <FormItem label="currencyTo" hasFeedback {...formItemLayout}>
          {getFieldDecorator('currencyCodeTo', {
            initialValue: item.currencyCodeTo,
            rules: [
              {
                required: true,
              },
            ],
          })(<Select style={{ width: '100%' }} {...SelectProps}>{typeData}</Select>)}
        </FormItem>
      </Col>

    </Row>)
  }
  return (
    <Modal {...modalOpts}>
      <Form layout="horizontal">
        {item.id ? createUpdateUI() : createNewUI()}
        {/* <Row className={style.FromandTo}> */}
        {/* {item.currencyCodeFrom} */}
        {/* <Icon type="swap-right" /> */}
        {/* {item.currencyCodeTo} */}
        {/* <hr className={style.modalcutLine} /> */}
        {/* </Row> */}
        <Row>
          <Col span={12}>
            <FormItem label="Buy T/T Offset" {...formItemLayout}>
              {getFieldDecorator('buyingBankRate1', {
                initialValue: item.buyingBankRate1,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<InputNumber step={0.001} style={{ width: '100%' }} />)}
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label="Buy Cash Offset" {...formItemLayout}>
              {getFieldDecorator('buyingRate1', {
                initialValue: item.buyingRate1,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<InputNumber step={0.001} style={{ width: '100%' }} />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="Sell T/T Offset" {...formItemLayout}>
              {getFieldDecorator('sellingBankRate1', {
                initialValue: item.sellingBankRate1,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<InputNumber step={0.001} style={{ width: '100%' }} />)}
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label="Sell Cash Offset" {...formItemLayout}>
              {getFieldDecorator('sellingRate1', {
                initialValue: item.sellingRate1,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<InputNumber step={0.001} style={{ width: '100%' }} />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="Sort" hasFeedback {...formItemLayout}>
              {getFieldDecorator('sort', {
                initialValue: item.sort,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Input />)}
            </FormItem>
          </Col>
        </Row>
        <hr className={style.modalcutLine} />
        <Row>
          <Col>
            <FormItem label="memo" {...memoLayout}>
              {getFieldDecorator('memo', {
                initialValue: item.memo,
              })(<TextArea />)}
            </FormItem>
          </Col>
        </Row>
      </Form>
    </Modal>
  )
}

modal.propTypes = {
  form: PropTypes.object.isRequired,
  type: PropTypes.string,
  item: PropTypes.object,
  onOk: PropTypes.func,
  currenciesData: PropTypes.array,
}

export default Form.create()(modal)
